package cn.kuang.controller;

import cn.kuang.pojo.Article;
import cn.kuang.service.ArticleService;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author 徽州大都督
 * @date 2020/7/24
 */
@RestController
@RequestMapping("article")
public class ArticleController {

    @Autowired
    ArticleService articleService;

    @PostMapping("add")
    public String addArticle(@RequestBody Article article){


        String content= Jsoup.parse(article.getArticleContent ()).text ();

        article.setAContent (content);
        //System.out.println (s);

        //System.out.println (article);
        article.setTid (UUID.randomUUID ().toString ());

        //处理标题长度
        if (article.getArticleTitle ().length ()>10){
            article.setArticleTitle (article.getArticleTitle ().substring (0,10));
        }

        //处理内容长度
        if (article.getArticleContent ().length ()>3000){
            article.setArticleContent (article.getArticleContent ().substring (0,3000));
        }

        article.setArticleTime (new Date ());

        int row = articleService.addArticle (article);

        if (row>0){
            return "success";
        }else {
            return "error";
        }


    }

    @GetMapping("findPage/{page}/{size}")
    public List<Article> findPage(@PathVariable("page")Integer page,@PathVariable("size")Integer size){

        //System.out.println (page);
        //System.out.println (size);
        List<Article> articles=articleService.findPage(page,size);

        return articles;
    }


    @GetMapping("findByTid/{tid}")
    public Article findByTid(@PathVariable("tid")String tid){

        //System.out.println (tid);
        Article article = articleService.findByTid (tid);
        //System.out.println (article);
        return article;
    }

}
